package com.zzdz.system.controller;

import com.zzdz.common.annotation.ControllerEndpoint;
import com.zzdz.common.controller.BaseController;
import com.zzdz.common.entity.DeptTree;
import com.zzdz.common.entity.QueryRequest;
import com.zzdz.common.entity.ZzdzConstant;
import com.zzdz.common.entity.ZzdzResponse;
import com.zzdz.common.exception.ZzdzException;
import com.zzdz.common.utils.ZzdzUtil;
import com.zzdz.system.entity.Dept;
import com.zzdz.system.entity.Dict;
import com.zzdz.system.service.IDictService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 字典表 Controller
 *
 * @author Zzdz
 * @date 2020-05-21 10:17:46
 */
@Slf4j
@Validated
@Controller
public class DictController extends BaseController {

    @Autowired
    private IDictService dictService;

    /**
    * 字典表列表视图
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dict")
    public String dictIndex(){
        return ZzdzUtil.view("system/dict/dictList.html");
    }

    /**
    * 获取所有字典表数据
    * @return
    */
    @GetMapping("dict")
    @ResponseBody
    @RequiresPermissions("dict:list")
    public ZzdzResponse getAllDicts(Dict dict) {
        return new ZzdzResponse().success().data(dictService.findDicts(dict));
    }

    /**
    * 获取分页字典表数据
    * @return
    */
    @GetMapping("dict/list")
    @ResponseBody
    @RequiresPermissions("dict:list")
    public ZzdzResponse dictList(QueryRequest request, Dict dict) {
        Map<String, Object> dataTable = getDataTable(this.dictService.findDicts(request, dict));
        return new ZzdzResponse().success().data(dataTable);
    }

    /**
    * 新增字典表视图
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dict/add")
    @RequiresPermissions("dict:add")
        public String addDict() {
        return ZzdzUtil.view("system/dict/dictAdd.html");
    }

    /**
    * 保存字典表
    * @return
    */
    @ControllerEndpoint(operation = "新增Dict", exceptionMessage = "新增Dict失败")
    @PostMapping("dict")
    @ResponseBody
    @RequiresPermissions("dict:add")
    public ZzdzResponse saveDict(@Valid Dict dict) {
        this.dictService.createDict(dict);
        return new ZzdzResponse().success();
    }

    /**
    * 编辑字典表视图
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dict/edit/{id}")
    @RequiresPermissions("dict:update")
    public String editDict(@PathVariable String id, Model model) {
        Dict dict = dictService.getById(id);
        model.addAttribute(dict);
        return ZzdzUtil.view("system/dict/dictEdit.html");
    }
    /**
    * 更新字典表
    * @return
    */
    @ControllerEndpoint(operation = "修改Dict", exceptionMessage = "修改Dict失败")
    @PostMapping("dict/update")
    @ResponseBody
    @RequiresPermissions("dict:update")
    public ZzdzResponse updateDict(Dict dict) {
        this.dictService.updateDict(dict);
        return new ZzdzResponse().success();
    }

    /**
    * 查看字典表
    * @return
    */
    @GetMapping(ZzdzConstant.VIEW_PREFIX + "dict/view/{id}")
    @RequiresPermissions("dict:list")
    public String dictView(@PathVariable String id, Model model) {
        Dict dict = dictService.getById(id);
        model.addAttribute(dict);
        return ZzdzUtil.view("system/dict/dictView.html");
    }

    /**
    * 删除字典表
    * @return
    */
    @ControllerEndpoint(operation = "删除Dict", exceptionMessage = "删除Dict失败")
    @GetMapping("dict/delete")
    @ResponseBody
    @RequiresPermissions("dict:delete")
    public ZzdzResponse deleteDict(Dict dict) {
        this.dictService.deleteDict(dict);
        return new ZzdzResponse().success();
    }

    /**
    * 删除字典表
    * @return
    */
    @ControllerEndpoint(operation = "删除Dict", exceptionMessage = "删除Dict失败")
    @GetMapping("dict/delete/{ids}")
    @ResponseBody
    @RequiresPermissions("dict:delete")
    public ZzdzResponse deleteDicts(@PathVariable String ids) {
        this.dictService.deleteDictByIds(ids);
        return new ZzdzResponse().success();
    }

    /**
    * 导出字典表
    * @return
    */
    @ControllerEndpoint(operation = "修改Dict", exceptionMessage = "导出Excel失败")
    @PostMapping("dict/excel")
    @ResponseBody
    @RequiresPermissions("dict:export")
    public void export(QueryRequest queryRequest, Dict dict, HttpServletResponse response) {
        List<Dict> dicts = this.dictService.findDicts(queryRequest, dict).getRecords();
        ExcelKit.$Export(Dict.class, response).downXlsx(dicts, false);
    }

}
